package com.mallbook.controller.servlet.front;

import com.mallbook.model.pojo.Category;
import com.mallbook.model.pojo.User;
import com.mallbook.model.service.CartService;
import com.mallbook.model.service.CategoryService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author 宋明松
 * @Version V1.0.0
 * @Date 2022-07-05
 */
public class InitBarUtils {
    private static CategoryService categoryService = new CategoryService();
    private static CartService cartService = new CartService();

    public static void getInitBar(HttpServletRequest req, HttpServletResponse resp) {
        List<Category> categoryList = categoryService.getAllCategoryIsNotDel();
        req.setAttribute("categories", categoryList);

        List<Category> categoryTag = categoryService.getAllCategoryIsNotDelAndDisplay();
        req.setAttribute("categoryTags", categoryTag);

        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("USER");
        if (user == null) {
            req.setAttribute("allGoodsNum", "0");
        } else {
            Integer allGoodsNum = cartService.getGoodsNumByuId(user.getId());
            if (allGoodsNum == null) {
                req.setAttribute("allGoodsNum", "0");
            } else {
                req.setAttribute("allGoodsNum", allGoodsNum);

            }
        }

    }
}
